﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Send2SMTP
{
    partial class Macro
    {

        ///////////////////////////////////////////////////////////////////////////
        //
        // %MESSAGENUM
        //
        ///////////////////////////////////////////////////////////////////////////
        static string MessageNumber_Handler(MailThreadData threadData, Random rand, string[] args)
        {
            return (threadData.messageNo + 1).ToString();
        }

        ///////////////////////////////////////////////////////////////////////////
        //
        // %DATETIME
        //
        ///////////////////////////////////////////////////////////////////////////
        static string DateTime_Handler(MailThreadData threadData, Random rand, string[] args)
        {
            return DateTime.Now.ToString();
        }

        ///////////////////////////////////////////////////////////////////////////
        //
        // %UNICODETABLE(startCP,endCP,autoInc)
        //
        ///////////////////////////////////////////////////////////////////////////
        static string UnicodeTable_Handler(MailThreadData threadData, Random rand, string[] args)
        {
            StringBuilder builder = new StringBuilder();
            int startCP;
            int endCP;
            bool autoIncrement = false;

            if (args.Length == 3)
            {
                startCP = int.Parse(args[0], System.Globalization.NumberStyles.HexNumber);
                endCP = int.Parse(args[1], System.Globalization.NumberStyles.HexNumber);
                autoIncrement = bool.Parse(args[2]);

                if (autoIncrement)
                {
                    int offset = threadData.messageNo * (endCP - startCP + 1);
                    startCP += offset;
                    endCP += offset;
                }
            }
            else
            {
                throw new Exception("Invalid arguments for %UNICODETABLE macro");
            }

            //
            // Dump the table
            int tableSize = endCP - startCP + 1;
            builder.Append("\nUnicode table: U+" + startCP.ToString("X4") + " to U+" + endCP.ToString("X4"));
            for (int index = 0; index < tableSize; index++)
            {
                if ((index % 16) == 0)
                {
                    builder.Append("\n");
                }

                //
                // Filter out the ASCII control characters
                if (startCP + index < 32)
                {
                    builder.Append('?');
                }
                else
                {
                    builder.Append((char)(startCP + index));
                }
                builder.Append(' ');
            }
            builder.Append("\n");



            return builder.ToString();
        }

        ///////////////////////////////////////////////////////////////////////////
        //
        // %UNICODECHAR(codePoint,autoInc)
        //
        ///////////////////////////////////////////////////////////////////////////
        static string UnicodeChar_Handler(MailThreadData threadData, Random rand, string[] args)
        {
            StringBuilder builder = new StringBuilder();
            int startCP;
            bool autoIncrement = false;

            if (args.Length == 2)
            {
                startCP = int.Parse(args[0], System.Globalization.NumberStyles.HexNumber);
                autoIncrement = bool.Parse(args[1]);

                if (autoIncrement)
                {
                    startCP = threadData.messageNo + startCP;
                }
            }
            else
            {
                throw new Exception("Invalid arguments for %UNICODECHAR macro");
            }

            //
            // Add the character (but filter out NULL)
            if (startCP > 0)
            {
                builder.Append((char)startCP);
            }
            return builder.ToString();
        }

        ///////////////////////////////////////////////////////////////////////////
        //
        // %RANDOMTEXT(min,max)
        //
        ///////////////////////////////////////////////////////////////////////////
        static string RandomText_Handler(MailThreadData threadData, Random rand, string[] args)
        {
            int wordCount = 0;
            StringBuilder text = new StringBuilder();
            int rowSize = 0;
            string word = "";
            int minWords, maxWords;

            if (args.Length == 2)
            {
                minWords = int.Parse(args[0]);
                maxWords = int.Parse(args[1]);
            }
            else
            {
                throw new Exception("Invalid arguments for %RANDOMTEXT macro");
            }

            int adjustedMaxWords = (int)(minWords + ((double)(maxWords - minWords)) * rand.NextDouble());

            text.Append("\n");
            while ((wordCount < minWords) || (wordCount < adjustedMaxWords))
            {
                word = WordDictionary.dictionary[rand.Next() % WordDictionary.dictionary.Length];
                text.Append(word);
                text.Append(" ");
                rowSize += word.Length;
                if (rowSize > 80)
                {
                    text.Append("\n");
                    rowSize = 0;
                }
                wordCount++;
            }
            return text.ToString();
        }

        ///////////////////////////////////////////////////////////////////////////
        //
        // %THREADID
        //
        ///////////////////////////////////////////////////////////////////////////
        static string ThreadId_Handler(MailThreadData threadData, Random rand, string[] args)
        {
            return threadData.threadId.ToString();
        }
    }
}
